
package com.jf.cloud.multishop.controller;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.vo.ShopSubstituteSalesVO;
import com.jf.cloud.multishop.dto.ShopSubstituteSalesDTO;
import com.jf.cloud.multishop.model.ShopSubstituteSales;
import com.jf.cloud.multishop.service.ShopSubstituteSalesService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 代销设置表
 *
 * @author zz
 * @date 2022-06-22 17:09:12
 */
@RestController("multishopShopSubstituteSalesController")
@RequestMapping("/m/shop_substitute_sales")
@Tag(name = "代销设置表")
public class ShopSubstituteSalesController {

    @Autowired
    private ShopSubstituteSalesService shopSubstituteSalesService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取代销设置表列表" , description = "分页获取代销设置表列表")
    public ServerResponseEntity<PageVO<ShopSubstituteSales>> page(@Valid PageDTO pageDTO) {
        PageVO<ShopSubstituteSales> shopSubstituteSalesPage = shopSubstituteSalesService.page(pageDTO);
        return ServerResponseEntity.success(shopSubstituteSalesPage);
    }

    @GetMapping
    @Operation(summary = "获取代销设置表" , description = "获取代销设置表")
    public ServerResponseEntity<ShopSubstituteSalesVO> getByShopSubstituteSalesId() {
        Long shopId = AuthUserContext.get().getTenantId();
        return ServerResponseEntity.success(shopSubstituteSalesService.getByShopSubstituteSalesId(shopId));
    }

    @PostMapping
    @Operation(summary = "保存代销设置表" , description = "保存代销设置表")
    public ServerResponseEntity<Void> save(@Valid @RequestBody ShopSubstituteSalesDTO shopSubstituteSalesDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        shopSubstituteSalesDTO.setShopId(shopId);
        ShopSubstituteSales shopSubstituteSales = mapperFacade.map(shopSubstituteSalesDTO, ShopSubstituteSales.class);
        shopSubstituteSales.setShopSubstituteSalesId(null);
        //获取代销设置
        ShopSubstituteSalesVO shopSubstituteSalesId = shopSubstituteSalesService.getByShopSubstituteSalesId(shopId);
        if (Objects.isNull(shopSubstituteSalesId)){
            shopSubstituteSalesService.save(shopSubstituteSales);
        }else {
            shopSubstituteSalesService.update(shopSubstituteSales);
        }
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新代销设置表" , description = "更新代销设置表")
    public ServerResponseEntity<Void> update(@Valid @RequestBody ShopSubstituteSalesDTO shopSubstituteSalesDTO) {
        ShopSubstituteSales shopSubstituteSales = mapperFacade.map(shopSubstituteSalesDTO, ShopSubstituteSales.class);
        shopSubstituteSalesService.update(shopSubstituteSales);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除代销设置表" , description = "根据代销设置表id删除代销设置表")
    public ServerResponseEntity<Void> delete(@RequestParam Long shopSubstituteSalesId) {
        shopSubstituteSalesService.deleteById(shopSubstituteSalesId);
        return ServerResponseEntity.success();
    }
}
